import {
  mangerstudent, // 添加学生接口
  mangerstudentedit, // 更新学生信息接口
  mangerstudentid, // 删除学生接口
  addmangerroom, // 添加教室接口
  mangerroomupdate, // 更新教室接口
  mangerroomdelete, // 删除教室接口
  mangergrade, // 添加班级接口
  mangergradeupdate, // 更新班级信息
  mangergradedelete, // 删除班级接口
  getmangergrade, // 获取已经分配教室的班级的接口
  mangerroom, // 获取全部教室
  getmangerstudent, // 已经分班学生接口
  mangerstudentnew, // 没有分班学生接口
  // mangergradenew // 获取没有分配教室的班级
  fetchSubject // 全部课程
} from '@/services/classmanagement'
export default {

  namespace: 'studentmanagement',

  state: {
    getmangerstudentList: [],
    mangerroomList: [],
    getmangergradeList: [],
    searchList: []
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
      dispatch({
        type: 'getmangerstudent'
      })
    },
  },

  effects: {
    *getmangerstudent({ payload }, { call, put }) {
      const promiseAll = [
        mangerroom(),
        fetchSubject(),
        mangerstudentnew()
      ]
      const initData = () =>Promise.all(promiseAll)
      const res = yield call(initData)
      console.log(res[2],'res.................')
      yield put({
        type: 'changeState',
        payload: {
          mangerroomList: res[0].data,
          getmangergradeList: res[1].data,
          getmangerstudentList: res[2].data
        }
      })
    },
    *mangerstudentid({ payload }, {call, put}){
      const data = yield call(mangerstudentid, payload)
      console.log(data)
      yield put({
        type: 'getmangerstudent'
      })
    },
    *SearchBtn({ payload }, { call, put }){
      const data = yield call(mangerstudentnew)
      const res = data.data
      if (payload.student_name ==undefined) {
        // eslint-disable-next-line no-const-assign
        const resault = res
        yield put({
          type: 'changeState',
          payload: {
            getmangerstudentList: resault
          }
        })
      }else{
        // eslint-disable-next-line no-const-assign
        const resault = res.filter(item=>item.student_name.includes(payload.student_name))
        yield put({
          type: 'changeState',
          payload: {
            getmangerstudentList: resault
          }
        })
      }
    },
    *mangerstudentedit({payload}, {call, put}){
      yield put({
        type: 'getmangerstudent'
      })
    }
  },

  reducers: {
    changeState(state, action) {
      return { ...state, ...action.payload };
    },
  },

};
